package com.huika.dealontop.control.homepage.activity;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.daimajia.swipe.SwipeLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.control.homepage.database.DindanDbHelper;
import com.huika.dealontop.support.domain.DindanBean;
import com.huika.dealontop.utils.DateTimeTool;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.TitleBarHelper;

import java.util.ArrayList;

/**
 * Created by cailu on 2015/12/1.
 */
public class DindanActivity extends BaseAct {


    private PullToRefreshListView mLv;
    private ArrayList<DindanBean> mAl;
    private DindanDbHelper dindanDbHelper;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_dindan);
    }

    class MyAdapter extends BaseAda<DindanBean> {
        public MyAdapter(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (convertView == null) {
                holder = new Holder();
                View inflate = View.inflate(getApplicationContext(), R.layout.item_dindan, null);
                holder.ivArrow = (ImageView) inflate.findViewById(R.id.homepage_iv_dindan_arrow);
                holder.ivPicture = (ImageView) inflate.findViewById(R.id.homepage_iv_dindanpicture);
                holder.tvContent = (TextView) inflate.findViewById(R.id.homepage_tv_dindancontent);
                holder.tvTime = (TextView) inflate.findViewById(R.id.homepage_tv_dindan_time);
                holder.sl = (SwipeLayout) inflate.findViewById(R.id.swipe);
                holder.delete = (Button) inflate.findViewById(R.id.delete);

                convertView = inflate;
                convertView.setTag(holder);


            } else {
                holder = (Holder) convertView.getTag();
            }
            final DindanBean item = getItem(position);

            if (item == null) {
                return convertView;
            }

            holder.tvContent.setText(item.getProductName());
            holder.isSelect = item.isSelected();
            holder.time = item.getTimes();
            holder.sl.setShowMode(SwipeLayout.ShowMode.LayDown);
            holder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new DindanDbHelper().deleteDate(item.getTimes());
                    date();
                }
            });

            if (item.isSelected()) {
                holder.tvContent.setTextColor(UIUtils.getColor(R.color.color_333333));
            } else {
                holder.tvContent.setTextColor(UIUtils.getColor(R.color.color_b1b1b1));
            }

            holder.tvTime.setText(DateTimeTool.dmyhmsDateTwo(item.getTimes()));


            if (item.getProductImageUrl() != null) {
                GlobalApp.loadImg(holder.ivPicture, item.getProductImageUrl());
            }

//            //点击跳转事件
//            convertView.setOnClickListener(DindanActivity.this);
//            //
            if (holder.ivArrow.getTag() == null) {
                holder.ivArrow.setOnClickListener(DindanActivity.this);
                holder.ivArrow.setTag(holder);
            }
            if (mExpandPosition == position && mExpandPosition != -1) {
                holder.ivPicture.setVisibility(View.VISIBLE);
                holder.tvContent.setMaxLines(1000);
                holder.ivArrow.setImageResource(R.mipmap.homepage_dindan_uparrow);
            } else {
                holder.ivPicture.setVisibility(View.GONE);
                holder.tvContent.setMaxLines(2);
                holder.ivArrow.setImageResource(R.mipmap.homepage_dindan_downarrow);
            }

            return convertView;
        }

        private int mExpandPosition = -1;

        public void setExpandPostion(int position) {
            mExpandPosition = position;
            notifyDataSetChanged();
        }

        public class Holder {
            ImageView ivArrow;
            ImageView ivPicture;
            TextView tvContent;
            TextView tvTime;
            boolean isSelect;
            long time;
            SwipeLayout sl;
            Button delete;
        }
    }

    ;

    @Override
    protected void initWidget() {
        super.initWidget();


        mLv = (PullToRefreshListView) findViewById(R.id.homepage_dindan_list);
        mAdapter = new MyAdapter(this);
        TitleBarHelper tbh = new TitleBarHelper(this, -1, -1, R.string.homepage_dindan_titlebar_message);
        tbh.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        CURRENT_PAGE = 10;

        mLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                CURRENT_PAGE = CURRENT_PAGE + PAGE_SIZE;
                mLv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        date();
                        mLv.onRefreshComplete();
                    }
                }, 1000);

            }
        });


        date();
        listener();
    }

    private void date() {
        //從數據庫中查詢出來,然後添加到list集合的javabean中.
        mAl = new DindanDbHelper().queryContent(CURRENT_PAGE);
        if (mAl.size() == 0) {
            showEmptyLayView(mLv, R.string.homepage_dindan_empty_message, -1);
        } else {
            hideEmptyLayView();
        }
        if (mAl.size() >= 10) {
            mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        } else {
            mLv.setMode(PullToRefreshBase.Mode.DISABLED);
        }
        mAdapter.setGroup(mAl);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
//            case R.id.homepage_ll_dindan:
//                break;
            case R.id.homepage_iv_dindan_arrow:
                MyAdapter.Holder holder = (MyAdapter.Holder) v.getTag();
                if (holder.ivPicture.getVisibility() == View.GONE) {
                    holder.ivPicture.setVisibility(View.VISIBLE);
                    holder.tvContent.setMaxLines(1000);
                    holder.ivArrow.setImageResource(R.mipmap.homepage_dindan_uparrow);
                } else {
                    holder.ivPicture.setVisibility(View.GONE);
                    holder.tvContent.setMaxLines(2);
                    holder.ivArrow.setImageResource(R.mipmap.homepage_dindan_downarrow);
                }
                if (holder.isSelect) {
                    //item.setSelected(false);
                    DindanDbHelper.getInstance().upDateSelected(holder.time + "", false);

                }

                break;
        }
    }

    private String mId = null;
    private MyAdapter mAdapter;

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void listener() {


        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DindanBean item = mAdapter.getItem(position - 1);
                mId = item.getProductId();
                mAdapter.setExpandPostion(position - 1);
                //将其变为false;
                if (item.isSelected()) {
                    //item.setSelected(false);
                    DindanDbHelper.getInstance().upDateSelected(item.getTimes() + "", false);

                }
            }
        });
    }
}
